﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    public partial class MainService : IFamily
    {
        public DataSet Family_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Family_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Family_GetList(out int ItemCount, int currentPage, int pageSize)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Family_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public FamilyInfo Family_GetInfo(int FamilyID)
        {
            FamilyInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Family_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Family_ID", FamilyID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new FamilyInfo();
                    retVal.ID = Convert.ToInt32(dr["Family_ID"]);
                    retVal.Name = dr["Family_Name"].ToString();
                    retVal.Description = dr["Family_Description"].ToString();
                    retVal.Address = dr["Family_Address"].ToString();
                    retVal.BirthYear = dr["Family_BirthYear"].ToString();
                    retVal.EmployeeID = Convert.ToInt32(dr["Family_EmployeeID"]);
                    retVal.IsDependent = Convert.ToBoolean(dr["Family_IsDependent"]);
                    retVal.Occupation = dr["Family_Occupation"].ToString();
                    retVal.Relationship = dr["Family_Relationship"].ToString();
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Family_GetWithEmployee(int EmployeeID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Family_GetWithEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Family_EmployeeID", EmployeeID);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Family_GetWithEmployee1(int EmployeeID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Family_GetWithEmployee1", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Family_EmployeeID", EmployeeID);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public int Family_Insert(FamilyInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Family_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Family_Relationship", eInfo.Relationship);
            dbCmd.Parameters.AddWithValue("@Family_Occupation", eInfo.Occupation);
            dbCmd.Parameters.AddWithValue("@Family_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@Family_IsDependent", eInfo.IsDependent);
            dbCmd.Parameters.AddWithValue("@Family_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@Family_Description", eInfo.Description);
            dbCmd.Parameters.AddWithValue("@Family_BirthYear", eInfo.BirthYear);
            dbCmd.Parameters.AddWithValue("@Family_Address", eInfo.Address);

            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool Family_Update(FamilyInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Family_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Family_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@Family_Relationship", eInfo.Relationship);
            dbCmd.Parameters.AddWithValue("@Family_Occupation", eInfo.Occupation);
            dbCmd.Parameters.AddWithValue("@Family_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@Family_IsDependent", eInfo.IsDependent);
            dbCmd.Parameters.AddWithValue("@Family_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@Family_Description", eInfo.Description);
            dbCmd.Parameters.AddWithValue("@Family_BirthYear", eInfo.BirthYear);
            dbCmd.Parameters.AddWithValue("@Family_Address", eInfo.Address);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool Family_UpdateEmployee(int EmployeeID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Family_UpdateEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool Family_Delete(int FamilyID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Family_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Family_ID", FamilyID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}

